import pygame, sys
from pygame.locals import *
from EvilGreg import *

class EventHandler(object):
    def __init__(self):
        None

    def doEvents(self, events):
        None
        
def waitForKeyOrQuit(key):
    event = pygame.event.wait()

    while True:
        if event.type == KEYDOWN and event.key == key:
            break
        elif event.type == KEYDOWN and event.key == K_ESCAPE:
            quit(0)
        elif event.type == QUIT:
            quit()
        else:
            event = pygame.event.wait() 

            
class GameEventHandler(EventHandler):



    QUIT_KEY         = K_ESCAPE
    ADD_KID_KEY      = K_k
    FPS_TOGGLE_KEY   = K_f
    PAUSE_KEY        = K_p
    MUSIC_TOGGLE_KEY = K_m
    
    UP_KEY    = K_UP
    DOWN_KEY  = K_DOWN
    LEFT_KEY  = K_LEFT
    RIGHT_KEY = K_RIGHT

    PUNCH_KEY = K_s
    KICK_KEY  = K_d
    EAT_KEY   = K_SPACE
    FLAME_KEY = K_a
    
    
    def __init__(self, game, gameObjectManager):
        EventHandler.__init__(self)
        self.game = game
        self.gObjMan = gameObjectManager
        
    def doEvents(self, events):
        for event in events:
            if event.type == QUIT:
                pygame.quit()
                sys.exit(0)         
            elif event.type == KEYDOWN:

                if event.key == self.QUIT_KEY:
                    pygame.quit()
                    sys.exit(0)            
                elif event.key == self.ADD_KID_KEY:
                    self.gObjMan.addRandomKid()
                elif event.key == self.FPS_TOGGLE_KEY:
                    self.game.hud.toggleFPS()
                    
                elif event.key == self.PAUSE_KEY:
                    self.game.pause = not self.game.pause #HACK
                elif event.key == self.MUSIC_TOGGLE_KEY:
                    self.game.toggleMusic()

                elif event.key == self.UP_KEY:
                    self.gObjMan.evilGreg.walk(EvilGreg.WALK_UP)
                elif event.key == self.DOWN_KEY:
                    self.gObjMan.evilGreg.walk(EvilGreg.WALK_DOWN)
                elif event.key == self.LEFT_KEY:
                    self.gObjMan.evilGreg.setFacing(EvilGreg.LEFT)
                    self.gObjMan.evilGreg.walk(EvilGreg.WALK_SIDE)
                elif event.key == self.RIGHT_KEY:
                    self.gObjMan.evilGreg.setFacing(EvilGreg.RIGHT)
                    self.gObjMan.evilGreg.walk(EvilGreg.WALK_SIDE)

                elif event.key == self.PUNCH_KEY:
                    self.gObjMan.evilGreg.punch()
                    
                elif event.key == self.KICK_KEY:
                    self.gObjMan.evilGreg.kick()
                    
                elif event.key == self.EAT_KEY:
                    self.gObjMan.evilGreg.eat()
                elif event.key == self.FLAME_KEY:
                    self.gObjMan.evilGreg.flame()
                    
            elif event.type == KEYUP:
                if event.key == self.UP_KEY:
                    self.gObjMan.evilGreg.stopWalk(EvilGreg.WALK_UP)
                elif event.key == self.DOWN_KEY:
                    self.gObjMan.evilGreg.stopWalk(EvilGreg.WALK_DOWN)
                elif event.key == self.LEFT_KEY:
                    self.gObjMan.evilGreg.stopWalk(EvilGreg.WALK_SIDE)
                elif event.key == self.RIGHT_KEY:
                    self.gObjMan.evilGreg.stopWalk(EvilGreg.WALK_SIDE)

